﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Text;

using System.Linq;






public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //调用自定义bindAudition方法，显示试听排行帮
            bindAudition();
            //调用自定义bindFashion方法，显示流行金曲
            bindFashion();
            //调用自定义bindOld方法，显示经典老歌
            bindOld();
            //调用自定义bindOccident方法，显示欧洲经典
            bindOccident();
            //调用自定义bindCampus方法，显示校园民谣
            bindCampus();
        }
    }
    //绑定GridView控件显示试听排行榜
    protected void bindAudition()
    { 
        string sqlSel = "select top 10 * from tb_musicInfo order by auditionSum desc";
        gvAudition.DataSource = dataoperate.getRows(sqlSel);
        gvAudition.DataKeyNames = new string[] { "id" };
        gvAudition.DataBind();
    }
    //绑定GridView控件显示流行金曲
    //流行音乐，经典老歌，欧洲经典,校园民谣，编号分别为1,2,3,4

    protected void bindFashion()
    {
        string sqlSel = "select top 7 * from tb_musicInfo where musicType=1 order by id desc ";
        gvFashion.DataSource = dataoperate.getRows(sqlSel);//返回一个记录集
        gvFashion.DataKeyNames = new string[] { "id" };
        gvFashion.DataBind();
    }
    //绑定GridView控件显示经典老歌
    protected void bindOld()
    {
        string sqlSel = "select top 7 * from tb_musicInfo where musicType=2  order by id desc ";
        gvOld.DataSource = dataoperate.getRows(sqlSel);
        gvOld.DataKeyNames = new string[] { "id" };
        gvOld.DataBind();
    }
    //绑定GridView控件显示欧洲经典
    protected void bindOccident()
    {
        string sqlSel = "select top 7 * from tb_musicInfo where musicType=3 order by id desc ";
        gvOccident.DataSource = dataoperate.getRows(sqlSel);
        gvOccident.DataKeyNames = new string[] { "id" };
        gvOccident.DataBind();
       
        
    }
    //绑定GridView控件显示校园民谣
    protected void bindCampus()
    {
        string selSel = "select top 7 * from tb_musicInfo where musicType=4 order by id desc ";
        gvCampus.DataSource = dataoperate.getRows(selSel);
        gvCampus.DataKeyNames = new string[] { "id" };
        gvCampus.DataBind();
        

    }




    /// <summary>
    /// 全选或全部取消GridView控件中的所有复选框
    /// </summary>
    /// <param name="gv">GridView对象，需要设置的GridView控件</param>
    /// <param name="bl">布尔值变量，设置选中还是取消</param>
    protected void allSelect(GridView gv, bool bl)
    {
        //判断传入的复选框对象是否为选中状态
        if (bl)
        {
            //遍历传入的GridView对象
            foreach (GridViewRow gvr in gv.Rows)
            {
                //设置GridView控件中的所有复选框为选中状态
                ((CheckBox)gvr.FindControl("CheckBox1")).Checked = false;
            }
        }
        else
        {
            foreach (GridViewRow gvr in gv.Rows)
            {
                //设置GridView控件中的所有复选框为不选中状态
                ((CheckBox)gvr.FindControl("CheckBox1")).Checked = true;
            }
        }
    }


    /// <summary>
    /// 判断GridView控件中的复选框是否被选中
    /// </summary>
    /// <param name="gv">GridView对象，需要判断的GridView控件</param>
    /// <returns>返回一个布尔值，如果复选框被选中返回True,否则返回False</returns>
    protected bool isCheck(GridView gv)  //判断是否有被选中的复选框,只要有被选中的c就为true,否则cflase
    {
        //创建布尔值变量并将变量设置为False
        bool c = false;
        //遍历GridView控件
        foreach (GridViewRow gvr in gv.Rows)
        {
            //判断复选框状态是否选中
            if (((CheckBox)gvr.FindControl("CheckBox1")).Checked)
            {
                //设置布尔值为Treu
                c = true;
                break;
            }
        }
        return c;
    }





    //获取选中传入到播放列表窗口中并播放
    /// <summary>
    /// 获取GridView控件中所有选中的歌曲id并传入到歌曲播放窗口中
    /// </summary>
    /// <param name="gv">GridView对象，需要获取的GridView控件</param>
    /// <param name="bl">布尔值表示是否有选中的复选框</param>
    protected void playList(GridView gv, bool bl)
    {
        //判断GridView控件中是否有选中的复选框
        if (bl)
        {
            //创建字符串变量
            string musicList = "";  //播放音乐列表
            //循环GridView控件
            for (int i = 0; i < gv.Rows.Count; i++)
            {
                //判断复选框是否为选中状态
                if (((CheckBox)gv.Rows[i].FindControl("CheckBox1")).Checked)
                {
                    //获取GridView控件的主键
                    musicList += gv.DataKeys[i].Value.ToString() + ",";  // 读出的是id的值
                }
            }
            //使用javaScript打开播放音乐列表窗口
            Response.Write("<script>window.open('playListMusic.aspx?id=" + musicList + "','','width=380,height=260')</script>");
        }
        else
        {
            RegisterStartupScript("", "<script>alert('请选择需要播放的歌曲！')</script>");
        }
    }



    //全选或取消歌曲排行榜中的歌曲
    protected void imgBtnAFull_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvAudition);

        allSelect(gvAudition, bl);
    }

    //全选或取消流行金曲中的歌曲
    protected void imgBtnFFull_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvFashion);

        allSelect(gvFashion, bl);
    }
    //全选或取消经典老歌中的歌曲
    protected void imgBtnOldFull_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvOld);

        allSelect(gvOld, bl);
    }
    //全选或取消欧美经典中的歌曲
    protected void imgBtnOcFull_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvOccident);

        allSelect(gvOccident, bl);
    }

    //全选或取消校园民谣中的歌曲
    protected void imgBtnCFull_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvCampus);

        allSelect(gvCampus, bl);

    }


    //播放所有选中的流行金曲
    protected void imgBtnFPlay_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvFashion);
        playList(gvFashion, bl);
    }
    //播放所有选中的歌曲排行榜
    protected void imgBtnAPlay_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvAudition);
        playList(gvAudition, bl);
    }
    //播放所有选中的经典老歌
    protected void imgBtnOldPlay_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvOld);
        playList(gvOld, bl);
    }
    //播放所有选中的欧美经典
    protected void imgBtnOcPlay_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvOccident);
        playList(gvOccident, bl);
    }
    //播放所有选中的校园民谣
    protected void imgBtnCPlay_Click(object sender, ImageClickEventArgs e)
    {
        bool bl = isCheck(gvCampus);
        playList(gvCampus, bl);
    }
}
